﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Resources;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ACAPM.CommonClass
{
    public class StringHelper
    {
        static string documentPath = HttpContext.Current.Server.MapPath("~/Content/Strings.xml");
            //Application.Path  "/Strings.xml";

        public static string GetDailyVerse(string key)
        {
            try
            {
                XDocument xdoc = XDocument.Load(documentPath);
                var query = from
                c in xdoc.Elements("messages")
                            .Elements("message")
                            where
                                c.Attribute("key")
                                 .Value == key
                            select c.Attribute("value").Value;
                if (query.Count() > 0)
                    return query.First();
                else
                    return string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static string UpdateSiteHitCount()
        {
            try
            {
                string filePath = HttpContext.Current.Server.MapPath("~/Content/SiteHitCount.xml");
                string totalCount = string.Empty;
                string sinceCount = string.Empty;
                string lastUpdated = string.Empty;
                XDocument xdoc = XDocument.Load(filePath);
                //var query = from
                //                c in xdoc.Elements("messages")
                //                            .Elements("message")
                //            where
                //                c.Attribute("key")
                //                 .Value == "SiteHitCount"
                //            select c.Attribute("value").Value;
                //if (query.Count() > 0)
                //    currentValue =  query.First();
                var query = from c in xdoc.Elements("messages").Elements("message")
                              
                            select c; 
             
                foreach (XElement mesg in query)
                {
                    mesg.Attribute("TotalHitCount").Value = Convert.ToString(Convert.ToInt32(mesg.Attribute("TotalHitCount").Value) + 1);
                    mesg.Attribute("SinceUpdateCount").Value = Convert.ToString(Convert.ToInt32(mesg.Attribute("SinceUpdateCount").Value) + 1);
                    totalCount = mesg.Attribute("TotalHitCount").Value;
                    sinceCount = mesg.Attribute("SinceUpdateCount").Value;
                    lastUpdated = mesg.Attribute("LastUpdated").Value;
                }
                
                xdoc.Save(filePath);
                return totalCount + "|" + sinceCount + "|" + lastUpdated;
            }
            catch (Exception ex)
            {

            }

            return string.Empty;
        }
    }
}
